package javajavado.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javajavado.clases.ConsultaPagos;
import javajavado.clases.Idea;
import javajavado.excepcion.DAOExcepcion;
import javajavado.util.ConexionBD;

public class ConsultaPagosDAO  extends BaseDAO {
	
	public Collection<ConsultaPagos> listar() throws DAOExcepcion {
		Collection<ConsultaPagos> c = new ArrayList<ConsultaPagos>();  //lo que se va a devolver
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT cn.nombrecentro, SUM(pg.monto) as montos FROM centro_formacion cn INNER JOIN pagos pg ON cn.idCentro_Formacion = pg.Centro_Formacion_idCentro_Formacion GROUP BY cn.nombrecentro";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				ConsultaPagos vo = new ConsultaPagos();
				vo.setNombrecentre(rs.getString("nombrecentro"));
				vo.setMonto(rs.getDouble("montos"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
}
